/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package wang.unclecat.dataability;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.bundle.ElementName;
import ohos.rpc.IRemoteObject;
import timber.log.Timber;
import wang.unclecat.dataability.alarm.list_ui.AlarmListSlice;
import wang.unclecat.mylibrary.multidevicekit.MultiDeviceHelper;
import wang.unclecat.mylibrary.multidevicekit.SelectDeviceDialog;

/**
 * Samples list ability slice, displays 4 buttons as entrance for 4 samples.
 *
 * @since 2021-05-15
 */
public class MainAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_main_ability_slice);

        connectService();

        // click and go to the Alarm List sample
        findComponentById(ResourceTable.Id_list_alarm_button)
                .setClickedListener(component -> present(new AlarmListSlice(),
                        new Intent().setParam("uri", "dataability:///wang.unclecat.datacenter.AlarmsDataAbility")));

        // click and go to the Alarm List sample
        findComponentById(ResourceTable.Id_list_alarm_button2)
                .setClickedListener(component -> go2AlarmListSlice());

        // click and go to the Alarm List sample
        findComponentById(ResourceTable.Id_list_alarm_button3)
                .setClickedListener(component -> {
                    new SelectDeviceDialog(getContext(), deviceInfo -> {

                        present(new AlarmListSlice(),
                                new Intent().setParam("uri",
                                        String.format("dataability://%s/wang.unclecat.datacenter.remote.AlarmsDataAbility", deviceInfo.getDeviceId())));

                    }).show();
                });

        // click and go to the Alarm List sample
        findComponentById(ResourceTable.Id_list_alarm_button_rdb)
                .setClickedListener(component -> present(new AlarmListSlice(),
                        new Intent().setParam("uri", "rdbstore:///wang.unclecat.datacenter.AlarmRdbStoreMetaDataHelper")));


        // click and go to the Alarm List sample
        findComponentById(ResourceTable.Id_list_alarm_button_service)
                .setClickedListener(component -> present(new AlarmListSlice(),
                        new Intent().setParam("uri", "dataservice:///wang.unclecat.testmetadatabinding")));

    }

    private void doGo2AlarmListSlice() {
        present(new AlarmListSlice(),
                new Intent().setParam("uri", "dataability:///wang.unclecat.datacenter.remote.AlarmsDataAbility"));
    }

    private boolean isConnected = false;
    private void go2AlarmListSlice() {
        if (isConnected) {
            doGo2AlarmListSlice();
        } else {
            new XPopup.Builder(getContext()).asConfirm("","打开DataCenter",  new OnConfirmListener() {
                @Override
                public void onConfirm() {
                    //通过打开一个page ability拉起应用进程
                    MultiDeviceHelper.openRemoteAbility(getAbility(),
                            "wang.unclecat.datacenter",
                            "wang.unclecat.datacenter.MainAbility");
                    isConnected = true;
                }
            }).show();
        }
    }

    private void connectService() {
        //这样不能拉起对方进程
        MultiDeviceHelper.connectService(this,
                "wang.unclecat.datacenter",
                "wang.unclecat.datacenter.ServiceAbility",
                new IAbilityConnection() {
                    @Override
                    public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int i) {
                        Timber.d("onAbilityConnectDone() called with: elementName = [ %s ], iRemoteObject = [ %s ], i = [ %s ]", elementName, iRemoteObject, i);
                        isConnected = true;
                    }

                    @Override
                    public void onAbilityDisconnectDone(ElementName elementName, int i) {
                        Timber.d("onAbilityDisconnectDone() called with: elementName = [ %s ], i = [ %s ]", elementName, i);
                        isConnected = false;
                    }
                });
    }

    @Deprecated
    private void pullDataCenter() {

        //通过打开一个page ability拉起应用进程
        MultiDeviceHelper.openRemoteAbility(getAbility(), "wang.unclecat.datacenter", "wang.unclecat.datacenter.MainAbility");

        //这样不能拉起对方进程
        MultiDeviceHelper.openRemoteAbility(getAbility(),
                "wang.unclecat.datacenter",
                "wang.unclecat.datacenter.ServiceAbility");

        //这样不能拉起对方进程
        MultiDeviceHelper.openRemoteAbility(getAbility(),
                "wang.unclecat.datacenter",
                "wang.unclecat.datacenter.AlarmsDataAbility");

        //这样不能拉起对方进程
        MultiDeviceHelper.connectService(this,
                "wang.unclecat.datacenter",
                "wang.unclecat.datacenter.ServiceAbility",
                new IAbilityConnection() {
                    @Override
                    public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int i) {
                        Timber.d("onAbilityConnectDone() called with: elementName = [ %s ], iRemoteObject = [ %s ], i = [ %s ]", elementName, iRemoteObject, i);
                    }

                    @Override
                    public void onAbilityDisconnectDone(ElementName elementName, int i) {

                    }
                });
    }

}
